import { Module } from 'vuex'
import { RootState } from '../types'
import { systemModule } from './types'
import {
  getPageListData,
  deletePageData,
  createPageData,
  editPageData
} from '@/api/main/system/system'

const pageUrlMap: any = {
  user: '/users/list',
  role: '/role/list',
  goods: '/goods/list',
  menu: '/menu/list'
}
const deletePageUrlMap: any = {
  user: '/users/',
  role: '/role/',
  goods: '/goods/',
  menu: '/menu/'
}
const createPageUrlMap: any = {
  user: '/users',
  role: '/role'
}

const editPageUrlMap: any = {
  user: '/users/',
  role: '/role'
}
const systemModule: Module<systemModule, RootState> = {
  namespaced: true,
  state: () => ({
    userList: [],
    userCount: 0,
    roleList: [],
    roleCount: 0,
    goodsList: [],
    goodsCount: 0,
    menuList: [],
    menuCount: 0
  }),
  mutations: {
    SET_USER_LIST(state, userList: any[]) {
      state.userList = userList
    },
    SET_USER_COUNT(state, userCount: number) {
      state.userCount = userCount
    },
    SET_ROLE_LIST(state, roleList: any[]) {
      state.roleList = roleList
    },
    SET_ROLE_COUNT(state, roleCount: number) {
      state.roleCount = roleCount
    },
    SET_GOODS_LIST(state, roleList: any[]) {
      state.goodsList = roleList
    },
    SET_GOODS_COUNT(state, roleCount: number) {
      state.goodsCount = roleCount
    },
    SET_MENU_LIST(state, menuList: any[]) {
      state.menuList = menuList
    },
    SET_MENU_COUNT(state, menuCount: number) {
      state.menuCount = menuCount
    }
  },
  actions: {
    getPageListAction({ commit }, payload: any) {
      const { pageName, queryInfo } = payload

      const pageUrl = pageUrlMap[pageName]
      getPageListData(pageUrl, queryInfo)
        .then((res) => {
          if (res) {
            const { list, totalCount } = res.data
            /*  list.forEach((item) => {
              progress进度条数据
              item.progress = 0
            }) */
            console.log(pageName.toUpperCase())
            commit(`SET_${pageName.toUpperCase()}_LIST`, list)
            if (totalCount) {
              commit(`SET_${pageName.toUpperCase()}_COUNT`, totalCount)
            } else {
              commit(`SET_${pageName.toUpperCase()}_COUNT`, list.length + 1)
            }
          }
        })
        .catch((error) => console.error(error))
    },
    // 删除逻辑
    async deletePageDataAction(context, payload: any) {
      // 1.pageName，id
      const { pageName, id } = payload
      const url = deletePageUrlMap[pageName] + id
      const res = await deletePageData(url)

      if (res.code != 0) {
        ElMessage({ message: res.data, type: 'error' })
      } else {
        ElMessage({ message: res.data, type: 'success' })
        // 重新请求列表数据
        context.dispatch('getPageListAction', {
          pageName: pageUrlMap[pageName],
          queryInfo: {
            offset: 0,
            size: 0
          }
        })
      }

      console.log(res)
    },
    // 新建逻辑
    async createPageDataAction(context, payload: any) {
      const { pageName, newData } = payload
      const pageUrl = createPageUrlMap[pageName]
      const result = await createPageData(pageUrl, newData)
      if (result.code == 0) {
        ElMessage({ message: result.data, type: 'success' })
        context.dispatch('getPageListAction', {
          pageName: pageUrlMap[pageName],
          queryInfo: {
            offset: 0,
            size: 0
          }
        })
      } else {
        ElMessage({ message: result.data, type: 'error' })
        return false
      }
    },
    // 修改逻辑
    async editPageDataAction({ dispatch }, payload: any) {
      const { pageName, editData, id } = payload
      const pageUrl = editPageUrlMap[pageName] + id
      const editRes = await editPageData(pageUrl, editData)

      if (editRes.code === 0) {
        ElMessage({ message: editRes.data, type: 'success' })
        dispatch('getPageListAction', {
          pageName: pageUrlMap[pageName],
          queryInfo: {
            offset: 0,
            size: 0
          }
        })
      } else {
        ElMessage({ message: editRes.data, type: 'error' })
      }
    }
  },
  getters: {
    pageListData(state) {
      return (pageName: string) => {
        // const listData: any[] = (state as any)[`${pageName}List`] ?? [] state有固定的类型 要转为any
        // return listData
        // todo 需要找更好的办法
        switch (pageName) {
          case 'user':
            return state.userList
          case 'role':
            return state.roleList
          case 'goods':
            return state.goodsList
          case 'menu':
            return state.menuList
        }
      }
    },
    pageListCount(state) {
      return (pageName: string) => {
        switch (pageName) {
          case 'user':
            return state.userCount
          case 'role':
            return state.roleCount
          case 'goods':
            return state.goodsCount
          case 'menu':
            return state.menuCount
        }
      }
    }
  }
}
export default systemModule
